<template>
    <div class="container">
        <div class="item flex" v-for="(item, index) in parseList" :key="index" v-show="item.isExpand">
            <div class="serial">{{item.index}}</div>
            <div class="deep" :style="{'padding-right': (item.deep * 16 + 'px')}"></div>
            <div class="nodeName" v-if="item.nodeName" >{{`${item.nodeName}`}}</div>
            <div  v-if="item.symbol" >{{`${item.symbol}`}}</div>
            <div class="nodeValue" style="padding-left:12px" :class="item.type">{{item.nodeContent || ''}}</div>
            <div  v-if="item.valueSymbol" >{{`${item.valueSymbol}`}}</div>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        json: Object|Array|String
    },
    data() {
        return {
            parseList: []
        }
    },
    watch: {
        json: {
            handler() {
                this.parseList = this.parseList2()
            },
            immediate: true,
            deep: true
        }
    },
    computed: {
        
    },
    methods: {
        parseList2() {
            let result = [];
            let index = 1;
            let type = typeof(this.json)
            if (type == 'string') {
                this.json = JSON.parse(this.json);
            } 
            let targetType = Object.prototype.toString.call(this.json);
            if (targetType == '[object String]') {
                this.json = JSON.parse(this.json);
            }
            
            if (targetType == '[object Array]') {
                result.push(this.arrayStart("", 0, index));
                this.generateArrayNodeContent(result, index, 0, this.json)
                result.push(this.arrayEnd(0, index, true));
            } else {
                this.loop(result, index, 0, this.json, true);
            }
            
            return result;
        },
  
        //生成基本类型节点的数据
        generateNormalNodeContent(result, startIndex, deep, node, value, isEnd) {
            result.push({index:startIndex, 
                        nodeName: node ? `"${node}"`: '', symbol: node ? ':' : '', deep: deep, type: typeof(value),
                        nodeContent: this.formatNodeContent(value), valueSymbol: isEnd ? '': ',', isExpand: true})
        },
        //生成对象类型节点的数据
        generateObjNodeContent(result, startIndex, deep, target) {
            deep++;
            return this.loop(result, startIndex, deep, target)
        },
        //生成数组类型节点的数据
        generateArrayNodeContent(result, startIndex, deep, target) {
            
            for(let i = 0; i < target.length; i++) {
                startIndex++;
                if (typeof(target[i]) == 'object') {
                    deep++;
                } else {
                    if (i == target.length - 1) {
                        startIndex ++;
                    }
                }
                startIndex = this.loop(result, startIndex, deep, target[i], i == target.length - 1)
            }
            return startIndex
        },
        objectStart(nodeName, deep, startIndex) {
            let symbol = '';
            if (nodeName) {
                nodeName = `"${nodeName}"`
                symbol = ': {'
            } else {
                nodeName = '';
                symbol = '{'
            }
            return {deep: deep, index: startIndex, nodeName: nodeName,  symbol: symbol,isExpand: true}
        },
        objectEnd(deep, startIndex, isEnd) {
            return {deep: deep, index: startIndex, symbol: '}' + (isEnd ? "" : " ,"), isExpand: true}
        },
        arrayStart(nodeName, deep, startIndex) {
            let symbol = '';
            if (nodeName) {
                nodeName = `"${nodeName}"`
                symbol = ': ['
            } else {
                nodeName = '';
                symbol = '['
            }
            return {deep: deep, index: startIndex, symbol: symbol, nodeName: nodeName, isExpand: true}
        },
        arrayEnd(deep, startIndex, isEnd) {
            return {deep: deep, index: startIndex, symbol: ']' + (isEnd ? '': ','), isExpand: true}
        },
        
        loop(result, index, deep, target, isEnd) {
            let length = -1;
            let targetType = Object.prototype.toString.call(target);
            let rangeStart;
            if (targetType == '[object Object]') {
                length = Object.keys(target).length;
                rangeStart = index;
                result.push(this.objectStart("", deep, index))
                index++;
            } else if(targetType == '[object Array]') {
                result.push(this.arrayStart("", deep, index));
                rangeStart = index;
                index++;
            } else {
                this.generateNormalNodeContent(result, index, ++deep, '', target, isEnd);
                return index;
            }
            let loopIndex = 0;
            for(let i in target) {
                let type = Object.prototype.toString.call(target[i])
                if (type == '[object Object]') {
                    //对象
                    let d2 = deep;
                    d2++;
                    result.push(this.objectStart(i, d2, index))
                    index++;
                    index = this.generateObjNodeContent(result, index, d2, target[i])
                    result.push(this.objectEnd(d2, index, loopIndex == length - 1))
                } else if (type == '[object Array]') {
                    //数组
                    let d2 = deep;
                    d2++;
                    result.push(this.arrayStart(i, d2, index))
                    index = this.generateArrayNodeContent(result, index, d2, target[i])
                    result.push(this.arrayEnd(d2, index, loopIndex == length - 1))
                } else {
                    //基本类型
                    let d2 = deep;
                    d2++;
                    this.generateNormalNodeContent(result, index, d2, i, target[i], loopIndex == length - 1);
                }
                index ++;
                loopIndex++;
            }
            if (targetType == '[object Object]') {
                result[rangeStart - 1]['range'] = {
                    start: rangeStart - 1,
                    end: index - 1
                }
                result[rangeStart - 1]['isExpand'] = true
                result.push(this.objectEnd(deep, index, isEnd));
                index++;
            } else if(targetType == '[object Array]') {
                result[rangeStart - 1]['range'] = {
                    start: rangeStart - 1,
                    end: index - 1
                }
                result[rangeStart - 1]['isExpand'] = true
                result.push(this.arrayEnd(deep, index, isEnd));
                index++;
            }
            return index;
        },
        formatNodeContent(target, isEnd) {
            let type = typeof(target);
            switch(type) {
                case 'string':
                    return `"${target}"${isEnd ? '' : ''}`
                default:
                    return target + (isEnd ? '' : "")    
            }
        }
    }
}
</script>
<style lang="less" scoped>
.container {
    background: #000;
    mix-blend-mode: difference
}
.item {
    display: flex;
    align-items: center;
    color: #FFF;
    font-size: 24px;
}
.serial {
    color: #fff;
    opacity: 0.8;
    height: 20px;
    width: 40px;
    line-height: 20px;
    text-align: center;
}
.deep {
    white-space: pre-line;
}
.nodeName {
    white-space: pre-line;
    color: red
}
.string {
    color: green
}
.number {
    color: blue;
}

</style>